package com.cungmua.app.task;

import android.content.Context;

import com.cungmua.app.handler.BaseRequestHandler;
import com.cungmua.app.handler.RegisterHandler;
import com.cungmua.app.model.request.RegisterRequest;
import com.cungmua.app.model.response.RegisterResponse;
import com.cungmua.app.net.CungMuaAPI;

import retrofit.RetrofitError;

/**
 * Created by kelvin.nguyen on 7/1/14.
 */
public class RegisterTask extends BaseRequestTask<RegisterRequest, Void, RegisterResponse> {

    private final Context context;
    private final RegisterHandler handler;

    public RegisterTask(Context context, RegisterHandler handler) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected RegisterResponse doInBackground() throws RetrofitError {
        RegisterRequest request = params[0];
        return CungMuaAPI.getService().register(request.toFieldMap());
    }

    @Override
    protected BaseRequestHandler getHandler() {
        return handler;
    }

    @Override
    protected void onSuccess(RegisterResponse registerResponse) {
        handler.onRegistered(registerResponse);
    }
}
